package com.glasses.manage.auth.controller;

import com.glasses.manage.auth.entity.User;
import com.glasses.manage.auth.entity.UserAddress;
import com.glasses.manage.auth.service.UserService;
import com.glasses.manage.export.DataField;
import com.glasses.manage.export.ExportDataSource;
import com.glasses.manage.export.excel.ExcelExportor;
import com.glasses.manage.export.excel.MODE;
import com.glasses.manage.utils.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


@RequestMapping(value = {"/user"})
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    private Logger logger = LoggerFactory.getLogger(UserController.class);


    @RequestMapping(value = "/error", method = RequestMethod.GET)
    public String error() {
        return "error";
    }

    /**
     * 用户列表管理
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String user_index() {
        return "user_list";
    }

    /**
     * 用户地址管理
     *
     * @return
     */
    @RequestMapping(value = "/addressIndex", method = RequestMethod.GET)
    public String userAddress_index() {
        return "user_address";
    }


    /**
     * 查询所有用户
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getUserList(String userName, String telphone) {
        List<User> userList = userService.getUserList(userName, telphone);
        return JSONUtil.object2Json(userList);
    }


    /**
     * 查询人员列表写入excel
     *
     * @return
     */
    @RequestMapping(value = "/excel", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String importEx(String userName, String telphone, HttpServletResponse response) {
        try {
            final List<User> rList = new ArrayList<User>();

            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String("用户列表.xls".getBytes("UTF-8"), "iso-8859-1"));
            ServletOutputStream out = response.getOutputStream();
            List<DataField> dataFieldList = new ArrayList<DataField>();
            dataFieldList.add(new DataField("用户名", "userName"));
            dataFieldList.add(new DataField("密码", "passWord"));
            dataFieldList.add(new DataField("昵称", "niChen"));
            dataFieldList.add(new DataField("手机号", "telphone"));
            dataFieldList.add(new DataField("省", "province"));
            dataFieldList.add(new DataField("市", "city"));
            dataFieldList.add(new DataField("头像链接", "touXiang"));
            dataFieldList.add(new DataField("等级", "isVIP"));
            dataFieldList.add(new DataField("账户余额", "account", true));
            dataFieldList.add(new DataField("状态", "recStatus"));
            dataFieldList.add(new DataField("最近登录", "loginTime"));

            List<User> userList = userService.getUserList(userName, telphone);
            for (int i = 0; i < userList.size(); i++) {
                if (userList.get(i).getIsVIP().equals("1")) {
                    userList.get(i).setIsVIP("尊贵会员");
                } else {
                    userList.get(i).setIsVIP("非会员");
                }
                if (userList.get(i).getRecStatus().equals("1")) {
                    userList.get(i).setRecStatus("启用");
                } else {
                    userList.get(i).setRecStatus("停用");
                }
            }
            rList.addAll(userList);
            new ExcelExportor<User>(dataFieldList, new ExportDataSource<User>() {
                public List<User> getData() {
                    return rList;
                }
            }, out, MODE.EXCEL).export();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 查询用户的地址
     *
     * @return
     */
    @RequestMapping(value = "/addressList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getUserAddressList(String userName) {
        List<UserAddress> userList = userService.getUserAddressList(userName);
        return JSONUtil.object2Json(userList);
    }


    /**
     * 查询人员地址列表写入excel
     *
     * @return
     */
    @RequestMapping(value = "/addressExcel", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String importAddreaaEx(String userName, HttpServletResponse response) {
        try {
            final List<UserAddress> rList = new ArrayList<UserAddress>();

            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String("用户收货地址列表.xls".getBytes("UTF-8"), "iso-8859-1"));
            ServletOutputStream out = response.getOutputStream();
            List<DataField> dataFieldList = new ArrayList<DataField>();
            dataFieldList.add(new DataField("用户名", "userName"));
            dataFieldList.add(new DataField("地址", "address"));
            dataFieldList.add(new DataField("是否默认", "isDefault"));
            dataFieldList.add(new DataField("状态", "recStatus"));

            List<UserAddress> userList = userService.getUserAddressList(userName);
            for (int i = 0; i < userList.size(); i++) {
                if (userList.get(i).getIsDefault().equals("1")) {
                    userList.get(i).setIsDefault("是");
                } else {
                    userList.get(i).setIsDefault("否");
                }
                if (userList.get(i).getRecStatus().equals("1")) {
                    userList.get(i).setRecStatus("启用");
                } else {
                    userList.get(i).setRecStatus("停用");
                }
            }
            rList.addAll(userList);
            new ExcelExportor<UserAddress>(dataFieldList, new ExportDataSource<UserAddress>() {
                public List<UserAddress> getData() {
                    return rList;
                }
            }, out, MODE.EXCEL).export();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
